<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#xA0;">
    <!ENTITY nl "&#xD;&#xA;">
]>

<!-- stylesheet for the basic templates that are applicable to all mimetypes -->

<!-- Version and namespaces -->
<xsl:stylesheet version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0"
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
    xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
    xmlns:int="http://opendocumentfellowship.org/internal"
    xmlns:math="http://www.w3.org/1998/Math/MathML"
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:ooo="http://openoffice.org/2004/office"
    xmlns:oooc="http://openoffice.org/2004/calc"
    xmlns:ooow="http://openoffice.org/2004/writer"
    xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
    xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    xmlns:xforms="http://www.w3.org/2002/xforms"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="anim chart config dc dr3d draw fo form int math meta number
        office ooo oooc ooow presentation script smil style svg table text xforms xlink #default">

    <!-- import the stylesheet for generating the CSS -->
    <xsl:import href="styles.xsl" />

    <!-- define the output content -->
    <xsl:output
        method="xml"
        indent="yes"
        omit-xml-declaration="yes"
        media-type="application/xhtml+xml"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        encoding="ISO-8859-1" />

    <!-- preserve the spaces contained on all elements -->
    <xsl:preserve-space elements="*" />

    <!-- set by javascript at runtime -->
    <xsl:param name="param_baseuri" />
    <xsl:param name="param_imgloc" />
    <xsl:param name="file_name" />

    <!-- set mimetype values for comparison -->
    <xsl:variable name="odt-mime">application/vnd.oasis.opendocument.text</xsl:variable>
    <xsl:variable name="ods-mime">application/vnd.oasis.opendocument.spreadsheet</xsl:variable>
    <xsl:variable name="odp-mime">application/vnd.oasis.opendocument.presentation</xsl:variable>

    <!-- represents the presentation's body -->
    <xsl:variable name="documentContent" select="/office:document/office:document-content" />
    <xsl:variable name="documentStyles" select="/office:document/office:document-styles" />

<!-- - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -->

    <!-- start at the document root -->
    <xsl:template match="/office:document">
        <html><xsl:text>&nl;</xsl:text>
            <head><xsl:text>&nl;</xsl:text>
                <xsl:apply-templates select="office:document-meta" /><xsl:text>&nl;</xsl:text>
                <xsl:call-template name="process-all-styles" /><xsl:text>&nl;</xsl:text>
            </head><xsl:text>&nl;</xsl:text>
            <xsl:choose>
                <xsl:when test="$mimetype=$odp-mime">
                    <xsl:call-template name="odp-js" /><xsl:text>&nl;</xsl:text>
                </xsl:when>
                <xsl:when test="$mimetype=$ods-mime">
                    <xsl:call-template name="ods-js" /><xsl:text>&nl;</xsl:text>
                </xsl:when>
            </xsl:choose>
            <xsl:apply-templates select="office:document-content" /><xsl:text>&nl;</xsl:text>
        </html>
    </xsl:template>

<!-- This section of the transformation handles generation of metadata. - - - - - - - - - - - - - - - - - - - - - - -->

    <xsl:template match="office:document-meta">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="office:meta">
        <xsl:comment> Metadata starts </xsl:comment><xsl:text>&nl;</xsl:text>
        <xsl:if test="not(dc:title)"> <!-- use the file name on the titlebar if there is not dc:title meta-tag -->
            <title><xsl:value-of select="$file_name" /></title><xsl:text>&nl;</xsl:text>
        </xsl:if>
        <xsl:apply-templates select="dc:title" mode="header" />
        <xsl:apply-templates select="dc:creator" mode="header" />
        <xsl:apply-templates select="dc:date" mode="header" />
        <xsl:apply-templates select="dc:language" mode="header" />
        <xsl:apply-templates select="dc:description" mode="header" />
        <xsl:apply-templates select="meta:keyword" mode="header" />
        <xsl:apply-templates select="meta:generator" mode="header" />
        <xsl:comment> Metadata ends </xsl:comment><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="dc:title" mode="header">
        <title><xsl:apply-templates /></title><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="dc:language" mode="header">
        <meta http-equiv="content-language" content="{current()}" /><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="dc:creator" mode="header">
        <meta name="author" content="{current()}" /><xsl:text>&nl;</xsl:text>
        <meta name="DC.creator" content="{current()}" /><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="dc:description" mode="header">
        <meta name="description" content="{current()}" /><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="dc:date" mode="header">
        <meta name="revised" content="{current()}" /><xsl:text>&nl;</xsl:text>
        <meta name="DC.date" content="{current()}" /><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="meta:keyword" mode="header">
        <meta name="keywords" content="{current()}" /><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="meta:generator" mode="header">
        <meta name="generator" content="{current()}" /><xsl:text>&nl;</xsl:text>
    </xsl:template>

<!-- This section of the transformation handles the generation of the CSS. - - - - - - - - - - - - - - - - - - - - - -->

    <xsl:template name="process-all-styles">
        <xsl:element name="style">
            <xsl:attribute name="type">text/css</xsl:attribute>
            <!-- /* li:before { display: marker; } */ not yet supported in Firefox 3 -->
            p, div { margin: 0px; padding: 0px; } /* ODF paragraphs, by default, don't have any line spacing. (use div to represent text:p) */
            a[href] { color: blue; text-decoration: underline; } /* put a default link style in, so that links are evident */
            h1, h2, h3, h4, h5, h6 { clear: both; }
            * { z-index: 1; word-wrap: break-word; line-height: 100%; }
            <xsl:if test="$mimetype!=$odt-mime">
                a[onclick] { color: blue; text-decoration: underline; cursor: pointer; }
            </xsl:if>
            <xsl:choose>
                <xsl:when test="$mimetype=$ods-mime">td, th { vertical-align: top; font-size: 10pt; }
                .toolTipMarker { color:red;float:right; }
                div.tooltipContents { display: none; white-space: normal; padding: 5px; margin: 10px; z-index: 100; }
                td.tooltip:hover { position: relative; }
                td.tooltip:hover div.tooltipContents { display: block; position: absolute; }
                </xsl:when>
                <xsl:otherwise>td, th { vertical-align: top; font-size: 12pt; }</xsl:otherwise>
            </xsl:choose>
            /* Document styles start */
            <xsl:apply-templates select="office:document-styles/office:styles" />
            /* Document styles end */
            /* Automatic styles start */
            <xsl:apply-templates select="office:document-styles/office:automatic-styles" />
            <xsl:apply-templates select="office:document-content/office:automatic-styles" />
            /* Automatic styles end */&nl;
            <xsl:call-template name="toc-styles" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="office:document-styles/office:automatic-styles">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="office:document-content/office:automatic-styles">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="office:document-styles/office:styles">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template name="toc-styles">
        <xsl:apply-templates select="//text:table-of-content" mode="toc-styles" />
    </xsl:template>

    <xsl:template match="text:table-of-content" mode="toc-styles">
        /* ToC styles start */&nl;.toc_outline a { text-decoration: none; }
        <xsl:apply-templates select="//text:h" mode="toc-styles" />
        /* ToC styles end */
    </xsl:template>

    <xsl:template match="text:h" mode="toc-styles">
        <xsl:if test="@text:outline-level">
            <xsl:text>&nl;.toc_outline_level_</xsl:text><xsl:value-of select="@text:outline-level" />
            <xsl:text>_</xsl:text><xsl:value-of select="@text:style-name" />
            <xsl:text> { margin-left: </xsl:text><xsl:value-of select="@text:outline-level*0.5" />
            <xsl:text>cm; }</xsl:text>
        </xsl:if>
    </xsl:template>

    <xsl:template match="*" mode="toc-styles" />


    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

    <!-- templates for generating a new ToC (instead of parsing the document's ToC) -->
    <xsl:template match="text:table-of-content">
        <xsl:comment> Table of Contents begins </xsl:comment>
        <div class="toc" style="width:100%; background-color:#D8D8D8; font-weight:bold;"><xsl:text>&nl;</xsl:text>
            <a name="toc" /><xsl:apply-templates select="text:index-body/text:index-title" />
            <xsl:apply-templates select="//text:h" mode="toc" />
        </div><xsl:comment> Table of Contents ends </xsl:comment><xsl:text>&nl;</xsl:text>
    </xsl:template>

    <xsl:template match="text:h" mode="toc">
        <xsl:element name="p">
            <xsl:attribute name="class">
                <xsl:text>toc_outline toc_outline_level_</xsl:text>
                <xsl:choose>
                    <xsl:when test="@text:outline-level">
                        <xsl:value-of select="@text:outline-level" />
                    </xsl:when>
                    <!-- ODF spec says that when unspecified the outline level should be considered to be 1. -->
                    <xsl:otherwise>1</xsl:otherwise>
                </xsl:choose>
                <xsl:text>_</xsl:text><xsl:value-of select="@text:style-name" />
            </xsl:attribute>
            <a href="#{generate-id()}"><xsl:value-of select="." /></a>
        </xsl:element><xsl:text>&nl;</xsl:text>
    </xsl:template>


<!-- This section of the transformation handles the generation of the body contents. - - - - - - - - - - - - - - - - -->

    <xsl:template match="office:document-content">
        <!-- approximation as attribute belongs to a page style, which won't work in XHTML -->
        <xsl:variable name="pageProperties" select="$documentContent/office:automatic-styles/style:page-layout[1]/style:page-layout-properties"/>

        <xsl:element name="body">
            <!-- direction of text flow -->
            <xsl:variable name="writingMode" select="$pageProperties/@style:writing-mode"/>
            <xsl:if test="$writingMode">
                <xsl:choose>
                    <xsl:when test="contains($writingMode, 'lr')">
                        <xsl:attribute name="dir">ltr</xsl:attribute>
                    </xsl:when>
                    <xsl:when test="contains($writingMode, 'rl')">
                        <xsl:attribute name="dir">rtl</xsl:attribute>
                    </xsl:when>
                </xsl:choose>
            </xsl:if>

            <!-- adapt page size -->
            <xsl:variable name="pageWidth" select="$pageProperties/@fo:page-width"/>

            <!-- multiple backgroundimages for different page styles (never used in html) -->
            <xsl:variable name="backgroundImage" select="$pageProperties/style:background-image"/>

            <!-- page margins & background image  -->
            <xsl:if test="$pageWidth or $pageProperties/@fo:* or $backgroundImage/@xlink:href">
                <xsl:attribute name="style">
                    <xsl:if test="$pageWidth">
                         <xsl:text>max-width:</xsl:text><xsl:value-of select="$pageWidth"/><xsl:text>;</xsl:text>
                    </xsl:if>
                    <xsl:if test="$pageProperties/@fo:* or $backgroundImage/@xlink:href">
                        <xsl:apply-templates select="$pageProperties/@fo:*"/>
                        <xsl:if test="$backgroundImage/@xlink:href">
                            <xsl:text>background-image:url(</xsl:text>
                            <xsl:call-template name="create-href">
                                <xsl:with-param name="href" select="$backgroundImage/@xlink:href"/>
                            </xsl:call-template>
                            <xsl:text>); </xsl:text>

                            <xsl:if test="$backgroundImage/@style:repeat">
                                <xsl:choose>
                                    <xsl:when test="$backgroundImage/@style:repeat = 'no-repeat'">
                                        <xsl:text>background-repeat: no-repeat; </xsl:text>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:text>background-repeat: repeat; </xsl:text>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:if>
                            <xsl:if test="$backgroundImage/@style:position">
                                <xsl:text>background-position: </xsl:text>
                                <xsl:value-of select="$backgroundImage/@style:position"/>
                                <xsl:text>; </xsl:text>
                            </xsl:if>
                        </xsl:if>
                    </xsl:if>
                </xsl:attribute>
            </xsl:if>

            <!-- evaluate the document's content -->
            <xsl:if test="$mimetype=$odp-mime">
                <xsl:attribute name="onload">showPage(1)</xsl:attribute><xsl:text>&nl;</xsl:text>
                <div style="text-align: right;" id="linkAll">
                <xsl:text> &nbsp; | &nbsp; </xsl:text>
                <a onclick="showAll()" style="font-family:Arial;font-size:10pt;">Show All Slides</a>
                <xsl:text> &nbsp; | &nbsp; &nl;</xsl:text>
                </div>
                <xsl:apply-templates select="office:body/office:presentation" />
            </xsl:if>
            <xsl:if test="$mimetype=$odt-mime">
                <xsl:apply-templates select="office:body/office:text" />
                <xsl:call-template name="add-footnote-bodies" />
            </xsl:if>
            <xsl:if test="$mimetype=$ods-mime">
                <xsl:variable name="oooViewSettings"
select="/office:document/office:document-settings/office:settings/config:config-item-set[@config:name='ooo:view-settings'][1]" />
                <xsl:variable name="activeSheet"
select="$oooViewSettings/config:config-item-map-indexed/config:config-item-map-entry/config:config-item[@config:name='ActiveTable'][1]" />
                <xsl:attribute name="onload">
                    <xsl:text>showPage('</xsl:text><xsl:value-of select="$activeSheet" /><xsl:text>')</xsl:text>
                </xsl:attribute><xsl:text>&nl;</xsl:text>
                <xsl:text> | </xsl:text>
                <xsl:for-each select="office:body/office:spreadsheet/table:table">
                    <xsl:element name="a">
                        <xsl:attribute name="onclick">
                            <xsl:text>showPage('</xsl:text><xsl:value-of select="@table:name" /><xsl:text>')</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="id">
                            <xsl:text>link</xsl:text><xsl:value-of select="@table:name" />
                        </xsl:attribute>
                        <xsl:attribute name="name"><xsl:text>sheetlink</xsl:text></xsl:attribute>
                        <xsl:value-of select="@table:name" />
                    </xsl:element>
                    <xsl:text> | </xsl:text>
                </xsl:for-each>
                <xsl:apply-templates select="office:body/office:spreadsheet" />
            </xsl:if>
            <xsl:text>&nl;</xsl:text>
        </xsl:element>
    </xsl:template>


<!-- - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -->

<!-- This section of the transformation handles the true content elements in the content.xml file
     (The order of the tags is based on the order on the OpenDocument specification.)

     /* templates for creating the body */
-->

<!-- HEADINGS, PARAGRAPHS AND BASIC TEXT STRUCTURE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

    <!-- template for converting the header tag <h> -->
    <xsl:template match="text:h">
        <xsl:choose>
            <xsl:when test="@text:outline-level >= 6">
                <xsl:element name="h6">
                    <xsl:if test="@text:style-name">
                        <xsl:attribute name="class">
                            <xsl:call-template name="callClassInheritance">
                                <xsl:with-param name="paramValue"><xsl:value-of select="@text:style-name" /></xsl:with-param>
                            </xsl:call-template>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:apply-templates />
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{concat('h', @text:outline-level)}">
                    <xsl:if test="@text:style-name">
                        <xsl:attribute name="class">
                            <xsl:call-template name="callClassInheritance">
                                <xsl:with-param name="paramValue"><xsl:value-of select="@text:style-name" /></xsl:with-param>
                            </xsl:call-template>
                        </xsl:attribute>
                    </xsl:if>
                    <a name="{generate-id()}" /> <!-- For the table-of-contents bookmarks -->
                    <a href="#toc" style="text-decoration:inherit; color:inherit" title="Return to Table of Contents">
                        <xsl:apply-templates />
                    </a>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text>&nl;</xsl:text>
    </xsl:template>

    <!-- template for recursive converting the paragraph tag <p> -->
    <xsl:template match="text:p">
        <xsl:param name="tempParam" />
        <xsl:text>&nl;</xsl:text>
        <xsl:choose>
            <xsl:when test="count(node())=0"><br /><!-- empty p tag yields a line break --></xsl:when>
            <xsl:otherwise>
                <!-- According to the HTML4 spec, you are not allowed to nest other block-level elements
                    (<div>, <ul>, other <p>, etc) inside a <p> without invalidating your HTML. -->
                <xsl:choose>
                    <xsl:when test="parent::text:list-item">
                        <xsl:element name="span">
                            <xsl:if test="@text:style-name">
                                <xsl:attribute name="class">
                                    <xsl:call-template name="callClassInheritance">
                                        <xsl:with-param name="paramValue"><xsl:value-of select="@text:style-name" /></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:apply-templates>
                                <xsl:with-param name="tempParam" select="$tempParam" />
                            </xsl:apply-templates>
                        </xsl:element>
                    </xsl:when>
                    <xsl:when test="parent::table:table-cell">
                        <xsl:element name="div">
                            <xsl:if test="$mimetype != $ods-mime">
                                <xsl:if test="$tempParam">
                                    <xsl:attribute name="style">width: <xsl:value-of select="$tempParam" /></xsl:attribute>
                                </xsl:if>
                            </xsl:if>
                            <xsl:if test="@text:style-name">
                                <xsl:attribute name="class">
                                    <xsl:call-template name="callClassInheritance">
                                        <xsl:with-param name="paramValue"><xsl:value-of select="@text:style-name" /></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:apply-templates>
                                <xsl:with-param name="tempParam" select="$tempParam" />
                            </xsl:apply-templates>
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:element name="div">
                            <xsl:if test="@text:style-name">
                                <xsl:attribute name="class">
                                    <xsl:call-template name="callClassInheritance">
                                        <xsl:with-param name="paramValue"><xsl:value-of select="@text:style-name" /></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:apply-templates>
                                <xsl:with-param name="tempParam" select="$tempParam" />
                            </xsl:apply-templates>
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text>&nl;</xsl:text>
    </xsl:template>

    <!-- templates for converting lists (uses recursion to handle lists in lists)-->
    <xsl:template match="text:list">
        <xsl:if test="not(../self::text:list)">
            <ul class="{concat(translate(@text:style-name,'.,;: %()[]/\+','_____________'), '_1')}">
                <xsl:text>&nl;</xsl:text>
                <xsl:call-template name="convertList">
                    <xsl:with-param name="level">1</xsl:with-param>
                    <xsl:with-param name="style_name">
                        <xsl:value-of select="@text:style-name" />
                    </xsl:with-param>
                </xsl:call-template>
            </ul>
        </xsl:if>
    </xsl:template>
    <xsl:template name="convertList">
        <xsl:param name="level" />
        <xsl:param name="style_name" />
        <xsl:for-each select="text:list-item">
            <xsl:element name="li">
                <xsl:if test="child::node()[1][self::text:list]">
                    <!-- if the first child of the <li> is a list, do not display the bullet for this <li> -->
                    <xsl:attribute name="style">list-style-type:none;</xsl:attribute>
                </xsl:if>
                <xsl:element name="span"> <!-- done to accommodate cases when color of bullet is different from text -->
                    <xsl:if test="$mimetype=$odp-mime">
                        <xsl:attribute name="class">
                            <xsl:call-template name="callClassInheritance">
                                <xsl:with-param name="paramValue"><xsl:value-of select="ancestor::draw:frame[1]/@presentation:style-name" /></xsl:with-param>
                            </xsl:call-template>
                            <xsl:if test="not(child::node()[1][self::text:list])"><xsl:text> </xsl:text>
                                <xsl:call-template name="callClassInheritance">
                                    <xsl:with-param name="paramValue"><xsl:value-of select="ancestor::draw:frame[1]/@draw:text-style-name" /></xsl:with-param>
                                </xsl:call-template>
                            </xsl:if>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:for-each select="*">
                        <xsl:choose>
                            <xsl:when test="self::text:list">
                                <ul class="{concat(translate($style_name,'.,;: %()[]/\+','_____________'), '_', ($level + 1))}">
                                    <xsl:call-template name="convertList">
                                        <xsl:with-param name="level">
                                            <xsl:value-of select="$level + 1" />
                                        </xsl:with-param>
                                        <xsl:with-param name="style_name">
                                            <xsl:value-of select="$style_name" />
                                        </xsl:with-param>
                                    </xsl:call-template>
                                </ul>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:apply-templates select="." />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </xsl:element>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

    <!-- templates for when the document has tracked changes on -->
    <xsl:template match="text:tracked-changes">
        <xsl:comment> Document has track-changes on </xsl:comment>
    </xsl:template>
    <xsl:template match="text:change">
        <xsl:variable name="id" select="@text:change-id" />
        <xsl:variable name="change" select="//text:changed-region[@text:id=$id]" />
        <!-- <del><xsl:value-of select="$change" /></del> -->
        <xsl:element name="del">
            <xsl:attribute name="datetime">
                <xsl:value-of select="$change//dc:date" />
            </xsl:attribute>
            <xsl:value-of select="$change//text:p" />
        </xsl:element>
    </xsl:template>


<!-- PARAGRAPH ELEMENTS CONTENT - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

    <!-- non-unicode display for ‘, ’, “, ”, – -->
    <xsl:template match="text()">
        <xsl:value-of
            select="translate(translate(translate(translate(translate(.,'–','&#45;'),'“','&quot;'),'”','&quot;'),'‘','&#145;'),'’','&#146;')" />
    </xsl:template>

    <!-- templates for converting the space tag -->
    <xsl:template match="text:s">
        <xsl:call-template name="preserve_spaces">
            <xsl:with-param name="count">
                <xsl:value-of select="@text:c" />
            </xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="preserve_spaces">
        <xsl:param name="count" />
        <xsl:if test="$count &gt; 0">
            <xsl:text>&nbsp;</xsl:text>
            <xsl:call-template name="preserve_spaces">
                <xsl:with-param name="count">
                    <xsl:value-of select="$count - 1" />
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <!-- template for converting the tab tag (replaced by 8 spaces since the tab char seems to be useless in html) -->
    <xsl:template match="text:tab">
        <xsl:text>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</xsl:text>
    </xsl:template>

    <!-- template for converting a line_break -->
    <xsl:template match="text:line-break">
        <br />
    </xsl:template>

    <!-- template for converting (Attributed Text) a span tag (contents are recursively parsed for templates) -->
    <xsl:template match="text:span">
        <xsl:param name="tempParam" />
        <xsl:element name="span">
            <xsl:if test="@text:style-name">
                <xsl:attribute name="class">
                    <xsl:call-template name="callClassInheritance">
                        <xsl:with-param name="paramValue"><xsl:value-of select="@text:style-name" /></xsl:with-param>
                    </xsl:call-template>
                </xsl:attribute>
            </xsl:if>
            <xsl:apply-templates>
                <xsl:with-param name="tempParam" select="$tempParam" />
            </xsl:apply-templates>
        </xsl:element>
    </xsl:template>

    <!-- template for converting hyperlinks -->
    <xsl:template match="text:a">
        <a name="{@office:name}" title="{@office:title}" href="{@xlink:href}"><xsl:apply-templates /></a>
    </xsl:template>

    <!--
        <text:bookmark-start /> and <text:bookmark-end /> can be on non-wellformed boundaries.
        The quickest solution is to create an <a name=""></a> element.

        If you don't put in any content, it becomes an empty element, which will confuse browsers.
        The right solution is to insert a zero-width non-breaking space (Unicode 0x200b) but this won't work
        with IE, so we use this instead.
    -->
    <xsl:template match="text:bookmark-start|text:bookmark">
        <a name="{@text:name}">
            <span style="font-size: 0px">
                <xsl:text> </xsl:text>
            </span>
        </a>
    </xsl:template>

    <!-- templates for the Note Element -->
    <xsl:template match="text:note">
        <xsl:variable name="footnote-id" select="text:note-citation" />
        <a href="#footnote-{$footnote-id}">
            <sup>
                <xsl:value-of select="$footnote-id" />
            </sup>
        </a>
    </xsl:template>

    <xsl:template name="add-footnote-bodies">
        <xsl:apply-templates select="//text:note" mode="add-footnote-bodies" />
    </xsl:template>

    <xsl:template match="text:note" mode="add-footnote-bodies">
        <xsl:variable name="footnote-id" select="text:note-citation" />
        <p><a name="footnote-{$footnote-id}"><sup><xsl:value-of select="$footnote-id" /></sup>:</a></p>
        <xsl:apply-templates select="text:note-body/*" />
    </xsl:template>

    <xsl:template match="text:note-body" />


<!-- TABLES - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

    <!-- template for converting the table tag -->
    <xsl:template match="table:table">
        <xsl:text>&nl;</xsl:text>
        <xsl:element name="table">
            <xsl:attribute name="id"><xsl:value-of select="@table:name" /></xsl:attribute>
            <xsl:if test="$mimetype=$ods-mime"><xsl:attribute name="name">spreadSheet</xsl:attribute></xsl:if>
            <xsl:variable name="tableName" select="@table:name" />
<xsl:variable name="tableAlign"
select="$documentContent/office:automatic-styles/style:style[@style:name=$tableName]/style:table-properties/@table:align" />
            <xsl:if test="$tableAlign"><xsl:attribute name="align"><xsl:value-of select="$tableAlign" /></xsl:attribute></xsl:if>
            <xsl:attribute name="class">
                <xsl:call-template name="callClassInheritance">
                    <xsl:with-param name="paramValue"><xsl:value-of select="@table:style-name" /></xsl:with-param>
                </xsl:call-template>
            </xsl:attribute>
            <colgroup>
                <xsl:apply-templates select="table:table-column" />
            </colgroup>
            <xsl:if test="table:table-header-rows/table:table-row">
                <thead>
                    <xsl:apply-templates select="table:table-header-rows/table:table-row" />
                </thead>
            </xsl:if>
            <tbody>
                <xsl:apply-templates select="table:table-row" />
            </tbody>
        </xsl:element>
    </xsl:template>

    <xsl:template match="table:table-column">
        <xsl:element name="col">
            <xsl:if test="@table:number-columns-repeated">
                <xsl:attribute name="span">
                    <xsl:value-of select="@table:number-columns-repeated" />
                </xsl:attribute>
            </xsl:if>
            <xsl:if test="@table:style-name">
                <xsl:attribute name="class">
                    <xsl:call-template name="callClassInheritance">
                        <xsl:with-param name="paramValue">
                            <xsl:value-of select="translate(@table:style-name,'.,;: %()[]/\+','_____________')" />
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:attribute>
            </xsl:if>
        </xsl:element>
    </xsl:template>

    <xsl:template match="table:table-row">
        <xsl:element name="tr">
            <xsl:apply-templates select="table:table-cell" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="table:table-cell">
        <xsl:variable name="n">
            <xsl:choose>
                <xsl:when test="@table:number-columns-repeated != 0">
                    <xsl:value-of select="@table:number-columns-repeated" />
                </xsl:when>
                <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:call-template name="process-table-cell">
            <xsl:with-param name="n" select="$n" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="process-table-cell">
        <xsl:param name="n" />
        <xsl:if test="$n != 0">
            <xsl:element name="td">
                <xsl:if test="@table:style-name">
                    <xsl:attribute name="class">
                        <xsl:call-template name="callClassInheritance">
                            <xsl:with-param name="paramValue">
                                <xsl:value-of select="translate(@table:style-name,'.,;: %()[]/\+','_____________')" />
                            </xsl:with-param>
                        </xsl:call-template>
                        <xsl:if test="child::node()[1][self::office:annotation]">
                            <xsl:text> tooltip</xsl:text>
                        </xsl:if>
                    </xsl:attribute>
                </xsl:if>
                <xsl:if test="not(@table:style-name) and child::node()[1][self::office:annotation]">
                    <xsl:attribute name="class">
                        <xsl:text>tooltip</xsl:text>
                    </xsl:attribute>
                </xsl:if>
                <xsl:if test="@table:number-columns-spanned">
                    <xsl:attribute name="colspan">
                        <xsl:value-of select="@table:number-columns-spanned" />
                    </xsl:attribute>
                </xsl:if>
                <xsl:if test="@table:number-rows-spanned">
                    <xsl:attribute name="rowspan">
                        <xsl:value-of select="@table:number-rows-spanned" />
                    </xsl:attribute>
                </xsl:if>
                <xsl:variable name="precSibs" select="count(preceding-sibling::table:covered-table-cell)"/>
                <xsl:variable name="colNumber" select="position() + $precSibs"/>
                <xsl:variable name="colStyleMain" select="ancestor::table:table[1]/table:table-column[1]/@table:style-name" />
                <xsl:variable name="colStyle" select="ancestor::table:table[1]/table:table-column[$colNumber]/@table:style-name" />
<xsl:variable name="thisWidth"
select="$documentContent/office:automatic-styles/style:style[@style:name=$colStyle][1]/style:table-column-properties/@style:column-width" />
<xsl:variable name="thisMainWidth"
select="$documentContent/office:automatic-styles/style:style[@style:name=$colStyleMain][1]/style:table-column-properties/@style:column-width" />
                <xsl:apply-templates>
                    <xsl:with-param name="tempParam">
                        <xsl:choose>
                            <xsl:when test="$thisWidth"><xsl:value-of select="$thisWidth" /></xsl:when>
                            <xsl:otherwise><xsl:value-of select="$thisMainWidth" /></xsl:otherwise>
                        </xsl:choose>
                    </xsl:with-param>
                </xsl:apply-templates>
                <!-- put a single space for an empty cell
                    (so that borders will still be drawn; esp. on firefox versions lower than 3.0) -->
                <xsl:if test="count(node())=0">&nbsp;</xsl:if>
            </xsl:element>
            <xsl:call-template name="process-table-cell">
                <xsl:with-param name="n" select="$n - 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>


<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

    <!-- helper templates to resolve class inheritance (Use Multiple Class Names and Combined Class Selectors) -->

    <xsl:template name="callClassInheritance">
        <xsl:param name="paramValue" />
        <xsl:call-template name="classInheritance">
            <xsl:with-param name="styleName"><xsl:value-of select="$paramValue" /></xsl:with-param>
            <xsl:with-param name="class"><xsl:value-of select="translate($paramValue,'.,;: %()[]/\+','_____________')" /></xsl:with-param>
            <xsl:with-param name="separator"><xsl:text> </xsl:text></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="classInheritance">
        <xsl:param name="styleName" />
        <xsl:param name="class" />
        <xsl:param name="separator" />
        <xsl:variable name="content-auto" select="$documentContent/office:automatic-styles/style:style[@style:name=$styleName][1]/@style:parent-style-name" />
        <xsl:variable name="styles-auto" select="$documentStyles/office:automatic-styles/style:style[@style:name=$styleName][1]/@style:parent-style-name" />
        <xsl:variable name="styles-styles" select="$documentStyles/office:styles/style:style[@style:name=$styleName][1]/@style:parent-style-name" />
        <xsl:choose>
            <xsl:when test="count($content-auto) > 0">
                <xsl:call-template name="classInheritance">
                    <xsl:with-param name="styleName">
                        <xsl:value-of select="$content-auto" />
                    </xsl:with-param>
                    <xsl:with-param name="class">
                        <xsl:value-of select="translate($content-auto,'.,;: %()[]/\+','_____________')" />
                        <xsl:value-of select="$separator" />
                        <xsl:value-of select="$class" />
                    </xsl:with-param>
                    <xsl:with-param name="separator">
                        <xsl:value-of select="$separator" />
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="count($styles-auto) > 0">
                <xsl:call-template name="classInheritance">
                    <xsl:with-param name="styleName">
                        <xsl:value-of select="$styles-auto" />
                    </xsl:with-param>
                    <xsl:with-param name="class">
                        <xsl:value-of select="translate($styles-auto,'.,;: %()[]/\+','_____________')" />
                        <xsl:value-of select="$separator" />
                        <xsl:value-of select="$class" />
                    </xsl:with-param>
                    <xsl:with-param name="separator">
                        <xsl:value-of select="$separator" />
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="count($styles-styles) > 0">
                <xsl:call-template name="classInheritance">
                    <xsl:with-param name="styleName">
                        <xsl:value-of select="$styles-styles" />
                    </xsl:with-param>
                    <xsl:with-param name="class">
                        <xsl:value-of select="translate($styles-styles,'.,;: %()[]/\+','_____________')" />
                        <xsl:value-of select="$separator" />
                        <xsl:value-of select="$class" />
                    </xsl:with-param>
                    <xsl:with-param name="separator">
                        <xsl:value-of select="$separator" />
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$class" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

<!-- - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -->

<!-- SHAPES & OBJECTS -->

    <xsl:template match="draw:circle | draw:polygon | draw:regular-polygon | draw:ellipse | draw:custom-shape |
            draw:text | draw:object">
        <!-- even if shapes cannot be drawn, at least show the text inside -->
        <xsl:element name="div">
            <xsl:attribute name="style">
                <xsl:if test="@svg:width">width: <xsl:value-of select="@svg:width" />; </xsl:if>
                <xsl:if test="@svg:height">height: <xsl:value-of select="@svg:height" />; </xsl:if>
                <xsl:if test="@svg:x">position: absolute; left: <xsl:value-of select="@svg:x" />; </xsl:if>
                <xsl:if test="@svg:y">top: <xsl:value-of select="@svg:y" />; </xsl:if>
                <xsl:if test="@draw:z-index">z-index: <xsl:value-of select="@draw:z-index" />; </xsl:if>
                <xsl:if test="not(@draw:z-index)">z-index: 5; </xsl:if>
            </xsl:attribute>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="draw:line | draw:polyline | draw:path | draw:g | draw:page-thumbnail | draw:measure |
            draw:caption | draw:connector | draw:control | dr3d:scene | draw:custom-shape | draw:text | draw:object">
        <!-- do nothing for now for shapes that we cannot draw -->
    </xsl:template>

</xsl:stylesheet>
